{ "cells": [ { "cell_type": "markdown", "id": "f2598a82", "metadata": {}, "source": [ "# Random Numbers" ] }, { "cell_type": "markdown", "id": "0c4fae8c", "metadata": {}, "source": [ "- Generate random data/numbers of any shape\n", " - rand generates any random data\n", " - randn generates data such that mean is 0 and Standard distribution is 1\n", " - randint generates integer data between your specified range" ] }, { "cell_type": "code", "execution_count": 14, "id": "352f87cb", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 8, "id": "c668e8ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.93385058, -0.67909051, -0.84774682])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randn(3) # 3 elements" ] }, { "cell_type": "code", "execution_count": 10, "id": "0a313eae", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2.50038775, -1.35721271, 1.18218949, 0.49287369],\n", " [-0.04991937, 0.21750541, 1.59958057, -2.55584519],\n", " [ 0.42088403, 0.88836931, 0.47062393, 1.86192363]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randn(3,4) # 3 rows x 4 cols" ] }, { "cell_type": "markdown", "id": "2a859372", "metadata": {}, "source": [ "## Examples" ] }, { "cell_type": "code", "execution_count": 13, "id": "58d2d09e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 4, 2, 4],\n", " [4, 3, 4, 4],\n", " [2, 1, 4, 1]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.randint(1,5,(3,4)) # Give me data from 1 to 5 of 3 rows and 4 cols" ] }, { "cell_type": "markdown", "id": "223bc509", "metadata": {}, "source": [ "# Convert arrays to dataframe" ] }, { "cell_type": "code", "execution_count": 15, "id": "9c548abf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>0</th>\n", " <th>1</th>\n", " <th>2</th>\n", " <th>3</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>2.453443</td>\n", " <td>-0.140874</td>\n", " <td>0.237828</td>\n", " <td>-0.179320</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>0.257964</td>\n", " <td>-0.363673</td>\n", " <td>1.252271</td>\n", " <td>2.127016</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>0.743469</td>\n", " <td>-0.178600</td>\n", " <td>-1.463442</td>\n", " <td>-0.747605</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " 0 1 2 3\n", "0 2.453443 -0.140874 0.237828 -0.179320\n", "1 0.257964 -0.363673 1.252271 2.127016\n", "2 0.743469 -0.178600 -1.463442 -0.747605" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(np.random.randn(3,4)) # 3x4=12 elements" ] }, { "cell_type": "markdown", "id": "fb8ebbd5", "metadata": {}, "source": [ "## Reshaping the data" ] }, { "cell_type": "code", "execution_count": 16, "id": "854c5739", "metadata": {}, "outputs": [], "source": [ "a=np.random.randint(1,5,(3,4)) # 4x3=12 elements" ] }, { "cell_type": "code", "execution_count": 17, "id": "b3af37fb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [1, 2],\n", " [3, 1],\n", " [3, 4],\n", " [3, 3],\n", " [1, 4]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.reshape(6,2) # 6x2=12 so it will generate data" ] }, { "cell_type": "code", "execution_count": 19, "id": "1efc7f3f", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "cannot reshape array of size 12 into shape (6,3)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [19]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m6\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 12 into shape (6,3)" ] } ], "source": [ "a.reshape(6,3) # it will through data because 6x3=18 elements and we don't have 18 elements" ] }, { "cell_type": "code", "execution_count": null, "id": "02c71b29", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }